package com.xsm.ppsecret.ui.pwd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xsm.ppsecret.R;
import com.xsm.ppsecret.model.PwdEntry;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PwdDetailActivity extends AppCompatActivity {

    @BindView(R.id.menu_normal_title)
    TextView menuTitle;
    @BindView(R.id.menu_normal_action)
    TextView menuAction;
    @BindView(R.id.pwd_detail_title)
    TextView title;
    @BindView(R.id.pwd_detail_username)
    TextView username;
    @BindView(R.id.pwd_detail_password)
    TextView password;
    @BindView(R.id.pwd_detail_remark)
    TextView remarks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_detail);
        ButterKnife.bind(this);
        menuTitle.setText("密码详情");
        menuAction.setVisibility(View.GONE);
        String json = getIntent().getStringExtra("json");
        if (json != null && !json.isEmpty()) {
            Gson gson = new Gson();
            PwdEntry pwdEntry = gson.fromJson(json, PwdEntry.class);
            String title = pwdEntry.getTitle();
            if (title != null && !title.isEmpty()) {
                this.title.setText(title);
            } else {
                this.title.setText("<空>");
            }
            String username = pwdEntry.getUsername();
            if (username != null && !username.isEmpty()) {
                this.username.setText(username);
            } else {
                this.username.setText("<空>");
            }
            String password = pwdEntry.getPassword();
            if (password != null && !password.isEmpty()) {
                this.password.setText(password);
            } else {
                this.password.setText("<空>");
            }
            String remarks = pwdEntry.getRemarks();
            if (remarks != null && !remarks.isEmpty()) {
                this.remarks.setText(remarks);
            } else {
                this.remarks.setText("<空>");
            }
        } else {
            Toast.makeText(this, "此数据可能丢失", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @OnClick(R.id.menu_normal_back) void back() {
        finish();
    }
}
